#include "stdafx.h"
#include "CombatCalculatorValidator.h"
#include "Combat/CombatSysIf.h"
#include "Combat/CombatSkill.h"
#include "Combat/CombatDefinition.h"
#include "Entity/EntityIf.h"


CombatCalculatorValidator::CombatCalculatorValidator(const CombatResult& s)
:m_rSvr(s)
{
}

bool CombatCalculatorValidator::doCalc(CombatSkill& ab, ICombatSys& c, ICombatSys& t, CombatResult& r)
{
	if (ab.m_nDamage>0)
	{
		return doCalcDamage(ab, c, t, r);
	}
	else if (ab.m_nDamage==0)
	{
		r.m_nResult = CombatResult::Succeed;
		r.m_nHPAffected = 0;
		r.m_bHasGene = false;
		r.m_bCriticalHit = false;
		return true;
	}
	else
	{
		return doCalcHeal(ab, c, t, r);
	}
}


bool CombatCalculatorValidator::doCalcDamage(CombatSkill& ab, ICombatSys& c, ICombatSys& t, CombatResult& r)
{
	/*if (t.hasImmunity(ab.m_nElement) != (m_rSvr.m_nResult==CombatResult::Immuned))
	{
		LOG_ERROR("Falied to validate combat result,  immunity dismatch!\n");
		return false;
	}

	if (ab.m_nDamage<0)
	{

	}

	if (m_rSvr.m_nResult!=CombatResult::Miss)
	{


		int nDamage = 0;
		if (m_rSvr.m_bCriticalHit)
		{
			nDamage = c.getToAttack(ab) * 3 - t.getToDefense(ab);
			r.m_bCriticalHit = true;
		}
		else
		{
			nDamage = c.getToAttack(ab) - t.getToDefense(ab);
			r.m_bCriticalHit = false;
		}

		if (nDamage<=0)
		{
			r.m_nHPAffected = -1;
		}
		else
		{
			r.m_nHPAffected = -nDamage;
		}

		if (r.m_nHPAffected==m_rSvr.m_nHPAffected)
		{
			return true;
		}
		else
		{
			LOG_ERROR("Falied to validate combat result,  damage value dismatch!\n");
			return false;
		}
	}
	else
	{
		r.m_nResult = CombatResult::Miss;
		return true;
	}*/
	return true;
}

bool CombatCalculatorValidator::doCalcHeal(CombatSkill& ab, ICombatSys& c, ICombatSys& t, CombatResult& r)
{
	r.m_nResult = CombatResult::Succeed;
	//
	//	Damage is the ratio on spirit
	r.m_bHasGene = false;
	r.m_bCriticalHit = false;

	if (m_rSvr.m_nResult!=CombatResult::Succeed || m_rSvr.m_nHPAffected!=r.m_nHPAffected)
	{
		LOG_ERROR("Falied to validate combat result,  heal value dismatch !\n");
		return false;
	}

	return true;
}